#!/bin/bash

killall wbar
killall tint2
killall openbox-menu

# blank root window to show openbox isn't running
hsetroot

# if we're switching to another GUI rather than just restarting
if ! grep "openbox-pandora-session" /tmp/gui.load > /dev/null
then
	# delete ON/OFF values for usb, bluetooth and wifi
  rm -f $XDG_CACHE_HOME/usb $XDG_CACHE_HOME/bluetooth $XDG_CACHE_HOME/wifi	

	# save our gtkrc
	cp $HOME/.gtkrc-2.0 $HOME/.gtkrc-2.0_openbox
	
	# give XFCE / MiniMenu their GTK+ theme back
	cp $HOME/.gtkrc-2.0_xfwm4 $HOME/.gtkrc-2.0

 	# give XFCE / MiniMenu their cache directory back
	export XDG_CACHE_HOME=$OPENBOX_OLD_CACHEDIR

	# if we're switching to XFCE, kill what is autostarts so
	# there isn't 2 of them
	if grep "startxfce4" /tmp/gui.load > /dev/null
	then	
		for i in /etc/xdg/autostart/*.desktop
		do 
			exec=$(grep ^Exec $i)
			pid=$(pidof "${exec:5}")
			if [ $pid ]
			then 
				kill -9 $pid; 
			fi
		done
	fi
fi

openbox --exit
